package com.zxw.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxw.common.web.annotation.InitSelectInfo;
import com.zxw.user.domain.dto.MenuDTO;
import com.zxw.user.domain.entity.Menu;
import com.zxw.user.mapper.MenuMapper;
import com.zxw.user.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-02-28
 */
@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    private final MenuMapper menuMapper;

    @Override
    public List<Menu> getMenuList(Menu menu) {
        QueryWrapper wrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(menu.getName()))
            wrapper.like("name", menu.getName());
        return menuMapper.selectList(wrapper);
    }

    @Override
    public List<MenuDTO> getMenuTableList(Menu menu) {
        List<Menu> menuList = getMenuList(menu);
        recursiveGenreateMenuTableList(0l,menuList);
        return null;
    }

    private List<MenuDTO> recursiveGenreateMenuTableList(Long parenetId, List<Menu> menuList) {
        List<MenuDTO> menuDTOS = new ArrayList<>();
        Optional.ofNullable(menuList).ifPresent(menus -> menus.stream().filter(menu -> menu.getParentId().equals(parenetId)).forEach(menu -> {
            MenuDTO menuDTO = new MenuDTO();
            BeanUtil.copyProperties(menu, menuDTO);
            List<MenuDTO> childrenList = recursiveGenreateMenuTableList(menuDTO.getId(), menuList);
            if (childrenList.size() > 0)
                menuDTO.setChildren(childrenList);

        }));
        return menuDTOS;
    }


}
